<template>
    <div id="Car">
        <nav-bar class="nav-bar">
            <div slot="center">→_→你买得起么({{myCar.length}})</div>
            <div slot="right">
                <input type="button" value="我的订单" class="orderBut" @click="orderClick">
            </div>
        </nav-bar>
        <car-list :carList="myCar" />
        <bottom-bar @createQrCode="createQrCode" />
        <el-dialog :title="paySucc?'支付成功':'扫码支付'" :visible.sync="dialogVisible" width="70%" class="visible">
            <!-- 生成二维码图片 -->
            <vueQr :text="qrCode" :size="200" v-if="!paySucc" center></vueQr>
            <!-- 使用websocket监控是否扫描，扫描成功显示成功并退出界面 -->
            <!-- <span class="iconfont icon-success" style="position: relative;font-size: 100px;color:#42B983;margin-left: 50px;top:-10px;" v-else>a</span> -->
            <span v-else class="tiaozhuan">2秒后跳转至订单页</span>
        </el-dialog>
    </div>
</template>

<script>
import NavBar from 'components/common/navbar/NavBar'
import CarList from './childComps/CarList'
import BottomBar from './childComps/BottomBar'
import vueQr from 'vue-qr'

import {selectCar, changeStatus} from 'network/shopcar.js'

export default {
    name: "Car",
    data() {
        return {
            qrCode: "",
            dialogVisible: false,
            paySucc: false,
            // 用于后面改变order支付状态暂时保存的
            OrderId: "",
        }
    },
    components: {
        NavBar,
        CarList,
        BottomBar,
        vueQr
    },
    computed: {
        myCar() {
            return this.$store.state.carList;
        }
    },
    created() {
        this._selectCar(this.$store.state.user[0].username);
    },
    methods: {
        _changeStatus(id) {
            changeStatus(id).then((res) => {
                console.info(res);
            })
        },
        orderClick() {
            this.$router.push("/shopcar/order");
        },
        _selectCar(userName) {
            selectCar(userName).then((res) => {
                console.info(res);
                this.$store.state.carList = res.data;
            })
        },
        _addOrder(obj) {
            addOrder(obj).then((res) => {
                console.info(res);
            })
        },
        createQrCode(map) {
            console.info(map)
            var obj = this;
            this.qrCode = map.qrCode;
            this.OrderId = map.orderId;

            this.dialogVisible = true;
            if ("WebSocket" in window) {
                // 打开一个 web socket
                var ws = new WebSocket("ws://localhost:9988/payment");

                ws.onopen = function() {
                    // Web Socket 已连接上，使用 send() 方法发送数据
                    ws.send("来自文刀毕设前端的贺电");
                    console.info("向服务端发送了数据");
                };

                ws.onmessage = function(evt) {
                    console.info("数据已接收..." + evt.data);
                    if (evt.data) {
                        obj.paySucc = true;
                        setTimeout(() => {
                            obj.dialogVisible = false;
                        }, 1500);
                        setTimeout(() => {
                            obj.$router.push("/shopcar/order");
                        }, 1500);
                        obj._changeStatus(obj.OrderId); 
                    }
                    ws.close();
                };
                    
                ws.onclose = function() {
                    // // 关闭 websocket
                    console.log("连接已关闭...");
                };
                // 需补充功能监听异常关闭连接，不知道有没有
                // window.onbeforeunload = this.onbeforeunload;
            } else {
                // 浏览器不支持 WebSocket
                this.$toast.show("您的浏览器不支持 WebSocket!",1000);
            }
        },
    }
}
</script>

<style scoped>
.nav-bar {
    position: relative;
    z-index: 9;
    background-color: greenyellow;
    color: #fff;
}
.orderBut {
    background-color: pink;
    color: #fff;
    width: 100px;
    height: 44px;
    text-align: center;
    line-height: 44px;
    float: right;

}
.tiaozhuan {
    color: red;
    font-size: 20px;
    text-align: center;
}
.visible{
    text-align: center;
}
</style>